package com.dream.keigezhushou.Activity.application;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.Process;
import android.support.multidex.MultiDex;


import com.squareup.okhttp.Cache;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;


public class MyApplication extends Application {

    public  Context context;
    public  Handler handler;
    public  int mainThreadId;

    public static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();

        instance = this;
        //Context 获取全局的context对象    new出一个View，加载布局文件，Toast
        context = getApplicationContext();
        handler = new Handler();
        /*new Thread(new Runnable() {
            @Override
            public void run() {
                Handler mHandler = new Handler(Looper.getMainLooper());
            }
        }).start();*/

        //判断当前线程是主线程还是子线程
        mainThreadId = Process.myTid();

       // initImageLoader(getApplicationContext());


//        DisplayImageOptions defaultOptions = new DisplayImageOptions
//                .Builder()
//                .showImageForEmptyUri(R.mipmap.ic_error_page)
//                .showImageOnFail(R.mipmap.dsdd)
//                .cacheInMemory(t)
//                .cacheOnDisc(t)
//                .build();
//        ImageLoaderConfiguration config = new ImageLoaderConfiguration
//                .Builder(getApplicationContext())
//                .defaultDisplayImageOptions(defaultOptions)
//                .discCacheSize(50 * 1024 * 1024)
//                .discCacheFileCount(100)
//                .writeDebugLogs()
//                .build();
//        ImageLoader.getInstance().init(config);


        //okhttp的配置缓存
//        File cacheFile = new File(getExternalCacheDir().toString(),"cache");
//        int cacheSize = 50 * 1024 * 1024 ;
//
//        Cache cache = new Cache(cacheFile,cacheSize);
//        OkHttpClient client = new OkHttpClient.Builder()
//                .cache(cache)
//                .build();


    }

    private ArrayList<Activity> activityArrayList = new ArrayList<>();

    public void addActivity(Activity activity) {
        activityArrayList.add(activity);
    }

    public void removeActivity(Activity activity) {
        activityArrayList.remove(activity);
    }
//    public static void initImageLoader(Context context) {
//        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
//        config.threadPriority(Thread.NORM_PRIORITY - 2);
//        config.denyCacheImageMultipleSizesInMemory();
//        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
//        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
//        config.tasksProcessingOrder(QueueProcessingType.LIFO);
//        config.writeDebugLogs(); // Remove for release app
//
//        // Initialize ImageLoader with configuration.
//        ImageLoader.getInstance().init(config.build());
//    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
